/*
  Example 21.1
 Texas Instruments PCF8574N demonstration sketch
 element-14 part number 7527718; RS part number 517-0687
 http://tronixstuff.com/tutorials > chapter 21
 CC by-sa v3.0
 */

#include "Wire.h"
#define redchip 0x20 // device addresses for PCF8547Ns on each LED colour bank 
#define yellowchip 0x22 //  addresses in this example match the published schematic in the tutorial
#define greenchip 0x21 // you will need to change addresses if you vary from the schematic
int dd=20; // used for delay timing

void setup()
{
  Wire.begin();
  allOff(); // the PCF8574N defaults to high, so this functions turns all outputs off
}

// remember that the IC "sinks" current, that is current runs fro +5v through the LED and then to I/O pin
// this means that 'high' = off, 'low' = on. 

void testfunc()
{
  Wire.beginTransmission(redchip);
  Wire.send(0); 
  Wire.endTransmission();
  delay(dd+50);
  Wire.beginTransmission(redchip);
  Wire.send(255); 
  Wire.endTransmission();
  delay(dd+50);
  Wire.beginTransmission(yellowchip);
  Wire.send(0); 
  Wire.endTransmission();
  delay(dd+50);
  Wire.beginTransmission(yellowchip);
  Wire.send(255); 
  Wire.endTransmission();
  delay(dd+50);
  Wire.beginTransmission(greenchip);
  Wire.send(0); 
  Wire.endTransmission();
  delay(dd+50);
  Wire.beginTransmission(greenchip);
  Wire.send(255); 
  Wire.endTransmission();
  delay(dd+50);
}

void testfunc2()
{
  for (int y=1; y<256; y*=2)
  {
    Wire.beginTransmission(redchip);
    Wire.send(255-y); // we need the inverse, that is high = off
    Wire.endTransmission();
    delay(dd);
    Wire.beginTransmission(redchip);
    Wire.send(255); 
    Wire.endTransmission();
    delay(dd);
  }
  
    for (int y=1; y<256; y*=2)
  {
    Wire.beginTransmission(yellowchip);
    Wire.send(255-y); 
    Wire.endTransmission();
    delay(dd);
    Wire.beginTransmission(yellowchip);
    Wire.send(255); 
    Wire.endTransmission();
    delay(dd);
  }
    for (int y=1; y<256; y*=2)
  {
    Wire.beginTransmission(greenchip);
    Wire.send(255-y); 
    Wire.endTransmission();
    delay(dd);
    Wire.beginTransmission(greenchip);
    Wire.send(255); 
    Wire.endTransmission();
    delay(dd);
  }
}

void testfunc3()
{
  Wire.beginTransmission(redchip);
  Wire.send(0); 
  Wire.endTransmission();
  Wire.beginTransmission(yellowchip);
  Wire.send(0); 
  Wire.endTransmission();
  Wire.beginTransmission(greenchip);
  Wire.send(0); 
  Wire.endTransmission();
  delay(dd+50);
  allOff();
  delay(dd+50);
}

void allOff()
{
  Wire.beginTransmission(redchip);
  Wire.send(255); 
  Wire.endTransmission();
  Wire.beginTransmission(yellowchip);
  Wire.send(255); 
  Wire.endTransmission();
  Wire.beginTransmission(greenchip);
  Wire.send(255); 
  Wire.endTransmission();
}

void loop()
{
 for (int z=0; z<10; z++)
 {
   testfunc();
 }
 for (int z=0; z<10; z++)
 {
   testfunc2();
 }
 for (int z=0; z<10; z++)
 {
   testfunc3();
 }
}



